<template>
  <div class="event-info">
    <el-form :model="ruleForm" :rules="rules" ref="RoleForm" label-width="100px" label-suffix="：">
      <div class="event-content">
        <el-form-item label="角色名称" prop="roleName">
          <div class="cont-input">
            <el-input v-model="ruleForm.roleName" placeholder="请输入角色名称"></el-input>
          </div>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <div class="cont-input">
            <el-input v-model="ruleForm.remark" type="textarea" placeholder="请输入备注"></el-input>
          </div>
        </el-form-item>
        <el-form-item label="状态" prop="isActivity">
          <div class="cont-input">
            <el-switch
              v-model="ruleForm.isActivity"
               active-value="1"
              inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </div>
        </el-form-item>
      </div>
      <div class="cont-footer">
        <el-button @click.native="eventTab('', 'first' )">取消</el-button>
        <template v-if="commonTabInfo.text === '新增角色'">
          <el-button type="primary" @click.native="addRoleEvent">保存</el-button>
        </template>
        <template v-else-if="commonTabInfo.text === '编辑角色'">
          <el-button type="primary" @click.native="editRoleEvent">保存编辑</el-button>
        </template>
      </div>
    </el-form>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { AddOrUpdateRole, GetRoleSingle } from '@/api/requestConfig'
  export default {
    name: 'RoleInfo',
    data() {
      return {
        ruleForm: {
          isActivity: '1',
          roleName: '',
          remark: ''
        },
        rules: {
          roleName: [
            { required: true, message: '请输入角色名称', trigger: 'blur' }
          ]
        },
        editData: {},
        addFlag: true
      }
    },
    computed: {
      ...mapGetters([
        'commonTabInfo'
      ])
    },
    created() {
      // 编辑时获取数据
      if (this.commonTabInfo.text === '编辑角色') {
        GetRoleSingle(this.commonTabInfo.custom.id).then(res => {
          if (res.data) {
            const resData = res.data
            this.editData = resData
            if (this.editData) {
              for (var key in this.editData) {
                if (key === 'isActivity') {
                  this.ruleForm[key] = String(this.editData[key])
                } else {
                  this.ruleForm[key] = this.editData[key]
                }
              }
            }
          }
        })
      }
    },
    methods: {
      // tabs操作
      eventTab(text, tabsName, custom) {
        this.$store.dispatch('handleTabInfo', {
          text: text,
          tabsName: tabsName,
          custom: custom
        })
      },
      // 新增角色
      addRoleEvent() {
        var self = this
        if (!this.addFlag) {
          return
        }
        this.$refs.RoleForm.validate(function(valid) {
          if (valid) {
            var needData = {}
            needData.roleName = self.ruleForm.roleName
            needData.remark = self.ruleForm.remark
            needData.isActivity = self.ruleForm.isActivity
            self.addFlag = false
            AddOrUpdateRole(needData).then(res => {
              if (res.code === 200) {
                self.$message.success(res.message)
                setTimeout(function() {
                  self.eventTab('refresh', 'first', 'role')
                }, 500)
              } else {
                self.$message.error(res.message)
              }
            })
          } else {
            return
          }
        })
        setTimeout(function() {
          self.addFlag = true
        }, 3000)
      },
      // 编辑
      editRoleEvent() {
        var self = this
        if (!this.addFlag) {
          return
        }
        this.$refs.RoleForm.validate(function(valid) {
          if (valid) {
            var needData = {}
            needData.roleName = self.ruleForm.roleName
            needData.remark = self.ruleForm.remark
            needData.isActivity = self.ruleForm.isActivity
            needData.id = self.editData.id
            self.addFlag = false
            AddOrUpdateRole(needData).then(res => {
              if (res.code === 200) {
                self.$message.success(res.message)
                setTimeout(function() {
                  self.eventTab('update', 'first', 'role')
                }, 500)
              } else {
                self.$message.error(res.message)
              }
            })
          } else {
            return
          }
        })
        setTimeout(function() {
          self.addFlag = true
        }, 3000)
      }
    }
  }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  @import "src/styles/info.scss";
</style>
